package com.gams.springboot.controller;

import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gams.springboot.pojo.Menu;
import com.gams.springboot.pojo.Role;
import com.gams.springboot.pojo.TreeMenu;
import com.gams.springboot.pojo.TreeMenu1;
import com.gams.springboot.services.MenuService;
import com.gams.springboot.services.RoleService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.web.servlet.ShiroHttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import springfox.documentation.spring.web.json.Json;

import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * @author tql
 * @current springboot
 * @create 2021-09-9-10:10
 */
@Controller
public class MenuController {
    @Autowired
    private MenuService menuService;

    @RequestMapping(value = "/init", method = RequestMethod.GET)
    @ResponseBody
    public Object showMenu(HttpSession session) {
        List<Menu> treeList = (List)session.getAttribute("menuList");
        List<TreeMenu1> treeMenuList = new ArrayList<>();


        for (Menu m : treeList) {
            if(m.getIsMenu()!=0){
                continue;
            }
            TreeMenu1 tree = new TreeMenu1();
            tree.setTitle(m.getName());
            tree.setId(m.getId());
            tree.setIcon(m.getImgUrl());
            tree.setHref(m.getAppUrl());
            tree.setChild(menuService.getTree1(m.getId()));
            treeMenuList.add(tree);

        }
        List<TreeMenu1> menuInfo = treeMenuList;
        Map<String,Object> indexInfo = new LinkedHashMap<>();
        TreeMenu1 logoInfo = new TreeMenu1();
        logoInfo.setTitle("权限管理系统");
        logoInfo.setImage("images/logo.png");
        TreeMenu1 homeInfo = new TreeMenu1();
        homeInfo.setTitle("首页");
        homeInfo.setHref("page/welcome-1.html");
        indexInfo.put("homeInfo",homeInfo);
        indexInfo.put("logoInfo",logoInfo);
        indexInfo.put("menuInfo",menuInfo);
        return  indexInfo;
    }


    @RequestMapping(value = "/menuList", method = RequestMethod.GET)
    @ResponseBody
    public List<TreeMenu> queryMenu() {
        return   menuService.getTree(0);
    }

    @RequestMapping(value = "/menu", method = RequestMethod.GET)
    @ResponseBody
    public Object queryAllMenu() {
        HashMap<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "加载成功");
        map.put("count", menuService.selectAllMenu().size());
        map.put("data", menuService.selectAllMenu());
        return map;
    }

    @PostMapping("/addMenu")
    @ResponseBody
    public int addMenu(@RequestBody Menu menu){
        return menuService.addMenu(menu);
    }

    @PostMapping("/updateMenu")
    @ResponseBody
    public int updateMenu(@RequestBody  Menu menu){
        return menuService.updateMenu(menu);
    }

    @PostMapping("/deleteMenu")
    @ResponseBody
    public int deleteMenu(@RequestParam long id){
        return menuService.deleteMenu(id);
    }

    @RequestMapping("/showMenu")
    public ModelAndView showMenu(@RequestParam(value="isMenu") String isMenu,
                                 @RequestParam(value="parentId") String parentId){
         ModelAndView v = new ModelAndView("/page/addmenu1.html");
         v.addObject("isMenu",Integer.parseInt(isMenu));
         v.addObject("parentId",Integer.parseInt(parentId));
        return v;
    }


    @RequestMapping("/editMenu")
    public ModelAndView edit(@RequestParam(value = "parentId")int parentId,
                             @RequestParam(value = "name")String name,
                             @RequestParam(value = "isMenu")String isMenu,
                             @RequestParam(value = "sortCode")String sortCode,
                             @RequestParam(value = "appUrl")String appUrl
    ){
        ModelAndView v = new ModelAndView("/page/editmenu.html");
        v.addObject("parentId" ,parentId);
        v.addObject("name",name);
        v.addObject("isMenu",isMenu);
        v.addObject("sortCode",sortCode);
        v.addObject("appUrl",appUrl);
        return v;
    }
}





